/**
 * Created by yuris on 21.01.15.
 */
package InterfaceObjects.BottomPanel {
import InterfaceObjects.ButtonLong;

import feathers.controls.Button;

import feathers.controls.Label;
import feathers.text.BitmapFontTextFormat;

import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import starling.filters.ColorMatrixFilter;

public class WarehouseItem extends Sprite {
    private var _skinImage:Image;
    private var _productImage:Image;
    private var _deliverTimeLabel:Label;
    private var _deliverTime:int = 0;;
    private var _bottomLabel:Label;
    private var _deliverNowButton:ButtonLong;
    public function WarehouseItem() {
        super();
        this._skinImage = GraphicFactory.getImageByName("Interface/Skins/bottom_panel_item_skin");
        this._skinImage.alignPivot();
        this._skinImage.width = 90;
        this._skinImage.height = 91;
        this.addChild(this._skinImage);

        this._deliverNowButton = new ButtonLong(ButtonLong.GREEN_TYPE);
        this._deliverNowButton.width = 80;
        this._deliverNowButton.height = 35;
        this._deliverNowButton.y = 35;
        this._deliverNowButton.x = 0;
        this._deliverNowButton.alignPivot()
        addChild(this._deliverNowButton)


        //deliver time label
        this._deliverTimeLabel = new Label();
        this._deliverTimeLabel.textRendererProperties.useSeparateBatch = false; //use separate if text color will change
        this._deliverTimeLabel.textRendererProperties.textFormat = new BitmapFontTextFormat("ComicDB_Dark",14)//, 0x333333);//color change causes extra Draw Call
        this._deliverTimeLabel.alignPivot();
        this._deliverTimeLabel.y = - 65;
        this._deliverTimeLabel.x = - 38;
        this._deliverTimeLabel.touchable = false;
        addChild(this._deliverTimeLabel);

        //bottom label
        this._bottomLabel = new Label();
        this._bottomLabel.textRendererProperties.useSeparateBatch = false; //use separate if text color will change
        this._bottomLabel.textRendererProperties.textFormat = new BitmapFontTextFormat("ComicDB_Dark",14)//, 0x333333);//color change causes extra Draw Call
        this._bottomLabel.alignPivot();
        this._bottomLabel.y = 25;
        this._bottomLabel.touchable = false;
        addChild(this._bottomLabel);

        this.addEventListener(TouchEvent.TOUCH, HANDLER_touch);

        this.alignPivot();//"left","top")

    }


    private var _bufTouch:Touch;
    private function HANDLER_touch(event:TouchEvent):void {
        if(this._deliverNowButton.visible){
            return;
        }

        this._bufTouch = event.getTouch(this);
        if(this._bufTouch){
            switch(this._bufTouch.phase){
                case TouchPhase.HOVER:
                    this._skinImage.filter = BottomPanel.itemHoverFilter;
                    break;
                case TouchPhase.BEGAN:
                    this._skinImage.filter = null;
                    break;
            }
        } else {
            this._skinImage.filter = null;
        }
    }




    private var _bufUint:uint;
    private var _bufStr:String;
    private var _deliverSecondsRest:int;
    public function set deliverSecondsRest(value:int):void {
        this._deliverSecondsRest = value
        if(value <= 0){
            this._deliverTimeLabel.visible = false;
            this._deliverNowButton.visible = false;
            this._bottomLabel.visible = true;

        } else{
            this._bufStr = "";
            this._bufUint = uint(value/3600) //hours;
            this._bufStr = this._bufUint + "h ";
            this._bufUint = uint(value/60) //minutes
            this._bufStr += this._bufUint + "m ";
            this._bufStr += (value - this._bufUint*60) + "s" //seconds

            this._deliverTimeLabel.visible = true;
            this._deliverTimeLabel.text = this._bufStr;

            this._deliverNowButton.visible = true;
            this._bottomLabel.visible = false;
        }
    }
    private var _currentImageName:String = null
    public function set productIconTexture(value:String):void {
        if(this._currentImageName != value){
            if(this._productImage != null && this._productImage.parent != null){
                this._productImage.parent.removeChild(this._productImage);
                this._productImage.dispose();

                this._productImage = null;
            }
            if(value != null){
                this._productImage = GraphicFactory.getImageByName(value);
                this._productImage.alignPivot();
                this._productImage.touchable = false;
                this.addChildAt(this._productImage,1);
            }
            this._currentImageName = value;
        }
        if(_productImage == null){//show order state
            this._deliverNowButton.visible = false;
            this._deliverTimeLabel.visible = false;
            this._bottomLabel.visible = false;

            this._productImage = GraphicFactory.getImageByName("Interface/Signs/sign_truck_big");
            this._productImage.alignPivot();
            this.addChild(this._productImage);
        }
    }
    public function set amount(value:String):void {
        this._bottomLabel.text = value;
        this._bottomLabel.visible = true;
    }


    public function get deliverNowButton():ButtonLong {
        return _deliverNowButton;
    }


    public function destruct():void {
        this.removeEventListener(TouchEvent.TOUCH, HANDLER_touch);

        if(parent){
            parent.removeChild(this);
            this.dispose();
        }
    }

    public function get deliverSecondsRest():int {
        return _deliverSecondsRest;
    }

    public function get deliverTime():int {
        return _deliverTime;
    }

    public function set deliverTime(value:int):void {
        _deliverTime = value;
    }
}
}
